#include <head.h>

void *pthread_write(void *write_fd)
{
    int fd = *((int *)write_fd);

    char buf[1024];

    while (1)
    {
        printf("输入信息\n");
        scanf("%d", buf);
        write(fd,buf,strlen(buf));

    }
    
}


void *pthread_read(void *read_fd)
{
    int fd = *((int*)read_fd);
    char buf[1024];
    memset(buf,0,sizeof(buf));
    int byte=0;
    while (1)
    {
        memset(buf,0,sizeof(buf));

        byte = read(fd,buf,sizeof(buf));

        if(byte == 0)
        {
            continue;
        }

        buf[strlen(buf)] = '\0';

        if(!strcmp(buf,"quite"))
        {
            printf("接收到关闭命令\n");
            return NULL;
        }

        printf("接收:%s strlen=l%d  byte = %d\n",buf,strlen(buf),byte);
    }
    
}



int main(int argc, char const *argv[])
{
    // 打开管道文件
    int fifo_1_fd = open("fifo_1" , O_RDONLY);
    int fifo_2_fd = open("fifo_2" , O_WRONLY);
    
    
    // 声明线程的id
    pthread_t tid_r; // 读线程
    pthread_t tid_w; // 写线程

    pthread_create(&tid_r, NULL, pthread_read, (void *)&fifo_1_fd);
    pthread_create(&tid_w, NULL, pthread_write, (void *)&fifo_2_fd);

    // 设定读线程分离态 
    pthread_detach(tid_w);

    // 回收线程 
    pthread_join(tid_r, NULL);

    // 当读线程结束 关闭写线程 
    pthread_cancel(tid_w);



    close(fifo_1_fd);
    close(fifo_2_fd);



    return 0;
}
